<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/views/global/taglibs.jsp" %>
<link rel="stylesheet" href="<%=basePath %>static/javascript/global/ztree/css/zTreeStyle/zTreeStyle.css" />

<div class="col-xs-4">
	<ul id="treeBody" class="ztree"></ul>
	<script type="text/javascript">
	 	
	 	     var setting = {
	 	    		check: {
	 					enable: true
	 				},
 	    		async: {
 					enable: true,
 					url:"admin/mallCategory/getDirectChilds",
 					autoParam:["id"],
 					otherParam:{"method":"get"},
 					dataFilter: filter
 				},	 
	 			
	 			view: {expandSpeed:"",
	 				addHoverDom: addHoverDom,
	 				removeHoverDom: removeHoverDom,
	 				selectedMulti: false
	 			},
	 			edit: {
	 				enable: true
	 			},
	 			data: {
	 				simpleData: {
						enable: true,
				        rootPId: -1
					
					}
	 			},
	 			callback: {
	 				beforeRemove: beforeRemove,
	 				beforeRename: beforeRename,
	 				beforeDrop: zTreeBeforeDrop
	 			}
	 		};
	 		
	 	     
	 	    function zTreeBeforeDrop(treeId, treeNodes, targetNode, moveType) {
	 	    	var treeObj = $.fn.zTree.getZTreeObj("treeBody");
	 	    	var nodes = treeObj.getSelectedNodes();
	 	    	var isDoMove=false;
	 	    	if(moveType == "prev"){
	 	    		if(targetNode!=null&&targetNode.pId==nodes[0].pId){
	 	    			isDoMove = true;
		 				$.ajax({
							type: "POST",
							async: false,
							url: "admin/mallCategory/reorder",
							data:{id: nodes[0].id,targetId:targetNode.id},
							dataType: "json",
							success: function(jsonData) {
								//confirmRet=true;
							}
						});
	 	    		}
	 	    	}
	 	       return isDoMove;
	 	   };
	 		
	 		function filter(treeId, parentNode, childNodes) {
	 			if (!childNodes) return null;
	 			for (var i=0, l=childNodes.length; i<l; i++) {
	 				childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
	 			}
	 			return childNodes;
	 		}
	 		
	 		function beforeRemove(treeId, treeNode) {
	 			var zTree = $.fn.zTree.getZTreeObj("treeBody");
	 			zTree.selectNode(treeNode);
	 			var confirmMsg =  "确认删除 节点 -- " + treeNode.name + " 吗？";
	 			var confirmRet=false;
	 			if(confirm(confirmMsg)){
	 				$.ajax({
						type: "POST",
						async: false,
						url: "admin/mallCategory/remove",
						data:{id: treeNode.id},
						dataType: "json",
						success: function(jsonData) {
							confirmRet=true;
						}
					});
	 			}
	 				
	 			return confirmRet;
	 		}
	 		
	 		function beforeRename(treeId, treeNode, newName) {
	 			$.ajax({
					type: "POST",
					url: "admin/mallCategory/rename",
					data:{id: treeNode.id,name:newName},
					dataType: "json",
					success: function(jsonData) {
						
					}
				});
	 			return true;
	 		}

	 		var newCount = 1;
	 		function addHoverDom(treeId, treeNode) {
	 			var sObj = $("#" + treeNode.tId + "_span");
	 			if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
	 			var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
	 				+ "' title='add node' onfocus='this.blur();'></span>";
	 			sObj.after(addStr);
	 			var btn = $("#addBtn_"+treeNode.tId);
	 			if (btn) btn.bind("click", function(){
					var zTree = $.fn.zTree.getZTreeObj("treeBody");
					$.ajax({
						type: "POST",
						url: "admin/mallCategory/add",
						data:{parentId: treeNode.id,name:"new node" + (newCount++)},
						dataType: "json",
						success: function(jsonData) {
							var addNodeJson =  jsonData.dataNode;
							zTree.addNodes(treeNode, addNodeJson);
						}
					});
					//zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
					return false;
				});
	 			
	 		};
	 		function removeHoverDom(treeId, treeNode) {
	 			$("#addBtn_"+treeNode.tId).unbind().remove();
	 		};
	 		
	 		

	 		$(document).ready(function(){
	 	
	 			$.fn.zTree.init($("#treeBody"), setting);
	 		
	 		});

	</script>
	
</div>
<div class="col-xs-8">
	<div id="frameDiv"></div>
</div>
<script type="text/javascript" src="<%=basePath %>static/javascript/global/ztree/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="<%=basePath %>static/javascript/global/ztree/js/jquery.ztree.excheck-3.5.js"></script>
<script type="text/javascript" src="<%=basePath %>static/javascript/global/ztree/js/jquery.ztree.exedit-3.5.js"></script>

